package com.hyzx.qbasic.admin.controller.system;

import com.github.pagehelper.PageInfo;
import com.hyzx.qbasic.admin.common.util.BaseResUtils;
import com.hyzx.qbasic.admin.controller.BaseController;
import com.hyzx.qbasic.admin.domain.service.ISysJobService;
import com.hyzx.qbasic.admin.model.annotation.SysLog;
import com.hyzx.qbasic.admin.model.constant.PermissionConst;
import com.hyzx.qbasic.admin.model.po.SysJobPO;
import com.hyzx.qbasic.admin.model.vo.req.admin.*;
import com.hyzx.qbasic.admin.model.vo.res.BaseOperationRes;
import com.hyzx.qbasic.admin.model.vo.res.BaseResModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.support.CronSequenceGenerator;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;

/**
 * 控制器类
 *
 * @author 黄江华
 * @since 2019-03-19
 */

@Api(tags = "定时任务", description = "SysJobController")
@Controller
@RequestMapping("/sys/job")
public class SysJobController extends BaseController {

    @Autowired
    private ISysJobService sysJobService;

    @ApiIgnore
    @SysLog("定时任务首页")
    @GetMapping("/list")
    public String list(Model model) {
        model.addAttribute("addFunc", getFunction(PermissionConst.SYS_JOB_ADD));
        model.addAttribute("editFunc", getFunction(PermissionConst.SYS_JOB_EDIT));
        model.addAttribute("deleteFunc", getFunction(PermissionConst.SYS_JOB_DELETE));
        return "system/job/list";
    }

    @ApiOperation("获取定时任务列表")
    @GetMapping("/getSysJobList")
    @ResponseBody
    public BaseResModel<PageInfo<SysJobPO>> getSysJobList(@Valid GetSysJobListReq req, BindingResult result) {
        return BaseResUtils.success(sysJobService.getPagedSysJobList(req));
    }

    @ApiIgnore
    @SysLog("定时任务新增页")
    @GetMapping("/add")
    public String add() {
        return "system/job/add";
    }

    @ApiOperation("定时任务新增表单提交")
    @PostMapping("/add")
    @ResponseBody
    public BaseResModel<BaseOperationRes> add(@Valid @RequestBody AddSysJobReq req, BindingResult result) {
        //校验cron表达式合法性
        boolean isCronExpressionValid = CronSequenceGenerator.isValidExpression(req.getCronExpression());
        if (!isCronExpressionValid) {
            return BaseResUtils.paramError("cron表达式无效");
        }

        BaseOperationRes res = sysJobService.addSysJob(req);
        return BaseResUtils.success(res);
    }

    @ApiOperation("删除定时任务记录")
    @PostMapping("/delete")
    @ResponseBody
    public BaseResModel<BaseOperationRes> delete(@Valid @RequestBody DeleteSysJobReq req, BindingResult result) {
        BaseOperationRes res = sysJobService.deleteSysJob(req);
        return BaseResUtils.success(res);
    }

    @ApiIgnore
    @SysLog("定时任务编辑页")
    @GetMapping("/edit")
    public String edit() {
        return "system/job/edit";
    }

    @ApiOperation("定时任务编辑表单提交")
    @PostMapping("/edit")
    @ResponseBody
    public BaseResModel<BaseOperationRes> edit(@Valid @RequestBody EditSysJobReq req, BindingResult result) {
        //校验cron表达式合法性
        boolean isCronExpressionValid = CronSequenceGenerator.isValidExpression(req.getCronExpression());
        if (!isCronExpressionValid) {
            return BaseResUtils.paramError("cron表达式无效");
        }

        BaseOperationRes res = sysJobService.editSysJob(req);
        return BaseResUtils.success(res);
    }

    @ApiOperation("获取定时任务信息")
    @GetMapping("/getSysJobInfo")
    @ResponseBody
    public BaseResModel<SysJobPO> getSysJobInfo(@Valid GetSysJobInfoReq req, BindingResult result) {
        return BaseResUtils.success(sysJobService.getSysJobInfoById(req));
    }

    @ApiOperation("切换定时任务运行状态")
    @PostMapping("/changeJobStatus")
    @ResponseBody
    public BaseResModel<BaseOperationRes> changeJobStatus(@Valid @RequestBody GetSysJobInfoReq req, BindingResult result) {
        BaseOperationRes res = sysJobService.changeJobStatus(req.getJobId());
        return BaseResUtils.success(res);
    }
}